﻿using ASPControlsExtended.Demo.Models;
using ASPControlsExtended.Demo.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ASPControlsExtended.Demo.Demos.GridViewDemo {
    public partial class GridViewDemo : System.Web.UI.Page {
        private IList<Stock> _products;
        private Services.ListService _listService;

        private IList<Stock> Quotes {
            get {
                if (_products != null) {
                    return _products;
                }

                _products = GetQuotes();

                return _products;

            }
        }

        private ListService ListService {
            get {
                if (_listService != null) {
                    return _listService;
                }

                _listService = new ListService();

                return _listService;
            }
        }

        private IList<Stock> GetQuotes() {
            return ListService.GetStocks();
        }

        protected void Page_Load(object sender, EventArgs e) {
            if (IsPostBack) {
                return;
            }

            DataBindGrid();

        }

        private void DataBindGrid() {
            gvPaging.DataSource = Quotes;
            gvPaging.DataBind();
        }

        private bool _disposed;
        public override void Dispose() {
            if (_disposed) {
                return;
            }

            if (_listService != null) {
                _listService.Dispose();
                _listService = null;
            }

            base.Dispose();
        }
    }
}